/**
 * 
 */
package com.bitBusy.manageyourriches.utilityClasses.pickers;

import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

/**
 * Class to set a time from time picker
 * @author SumaHarsha
 */
public class TimePickerFragment extends DialogFragment implements OnTimeSetListener {



	/**Listeners*/
	private TimeChangeListener myListener;

	/**Reference to the set hour*/
	private static int myHour;
	
	/**Reference to the set minute*/
	private static int myMinute;
	
	/**Current time*/
	private static Time now = new Time();
	
	/**Object where the time is to be written*/
	private TextView myTimeView;
	
	
	
	  @Override
	    public Dialog onCreateDialog(Bundle savedInstanceState) {
	        // Use the current time as the default values for the picker
	        int hour = now.hour;
	        int minute = now.minute;

	        // Create a new instance of TimePickerDialog and return it
	        return new TimePickerDialog(getActivity(), this, hour, minute,
	                DateFormat.is24HourFormat(getActivity()));
	    }

	    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
	    	
	    	myHour = hourOfDay;
	    	myMinute = minute;
			setChangedTimeOnView();
			notifyListeners();
		}
		
		/** Method to set the changed time on the given View object*/
		private void setChangedTimeOnView()
		{
			if (myTimeView != null)
			{
				if (myTimeView instanceof TextView)
				{
					((TextView)myTimeView).setText(getTime()); 
				}
				else if (myTimeView instanceof Button)
				{
					((Button)myTimeView).setText(getTime()); 
				}
				else if (myTimeView instanceof EditText)
				{
					((EditText)myTimeView).setText(getTime()); 
				}
			}
		}
		
		/** Method to get new time 
		 * @return time in StringBuilder format*/
		private StringBuilder getTime()
		{
			return new StringBuilder().append(displayFormat(myHour)).append(":").append(
						displayFormat(myMinute)).append(":").append(displayFormat(now.second));
		}
		
		
		/** Method to correct display format of time members 
		 * @param int time member
		 * @return corrected String value
		 */
		private String displayFormat(int the_timeMember) {
			if (the_timeMember >= 10)
			{
			   return String.valueOf(the_timeMember);
			}
			else
			{
			   return "0" + String.valueOf(the_timeMember);
			}
		}
		
		/**Method to specify Object to which new time has to be set
		 * @param the View object
		 * @param the application Context
		 * @return boolean indicating if successfully set*/
		public boolean setOnObject(TextView the_view)
		{
			if( the_view != null)
			{
				if (the_view instanceof TextView || the_view instanceof Button || the_view instanceof EditText)
				{
					myTimeView =  the_view;
					return true;
				}
			}
			return false;
		}
		
		/**Method to add listener
		 * @param the listener
		 */
		public void addListener(TimeChangeListener the_listener)
		{
			if (the_listener != null)
			{
				myListener = the_listener;
			}
		}
		
		/**Method to notify listeners of change*/
		private void notifyListeners()
		{
			if (myListener != null)
			{
				myListener.timeHasChanged(myTimeView);
			}
		}
}
